//
//  TravelsViewController.m
//  FairyDream
//
//  Created by dllo on 15/8/21.
//  Copyright (c) 2015年 dreams. All rights reserved.
//

#import "TravelsViewController.h"
#import "Define.h"
#import "HeaderView.h"
#import "TravelCell.h"
#import "DetailViewController.h"
#import "AsyncGETConnection.h"
#import "TravelsModel.h"
#import "WebCell.h"
#import "HeaderModel.h"
@interface TravelsViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) NSMutableArray *tableArray;
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) HeaderView *header;
@property (nonatomic, retain) NSMutableArray *headerArray;

@end

@implementation TravelsViewController

- (void)dealloc
{
    [_tableArray release];
    [_tableView release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tableArray = [NSMutableArray array];
        self.headerArray = [NSMutableArray array];
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor purpleColor];
    self.navigationController.navigationBar.translucent = NO;
    //self.navigationController.navigationBar.barTintColor = [UIColor greenColor];
    UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 375, 64)];
    UIImage *image = [UIImage imageNamed:@"shangbian.jpg"];
    imageV.image = image;
    [self.navigationController.navigationBar addSubview:imageV];
    [self createTableView];
    [self beginConnection];
}

- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Width, Height - 64)];
    _tableView.backgroundColor = [UIColor redColor];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView release];
    
    
    self.header = [[[HeaderView alloc] initWithFrame:CGRectMake(0, 0, _tableView.frame.size.width, 200)]autorelease];
    _tableView.tableHeaderView = _header;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    
    return _tableArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    TravelsModel *model = [_tableArray objectAtIndex:indexPath.row];
    if ([model.type integerValue] == 4) {
        static NSString *cellT = @"cellTravel";
        TravelCell *cellTravels = [tableView dequeueReusableCellWithIdentifier:cellT];
        if (!cellTravels) {
            cellTravels = [[[TravelCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellT]autorelease];
        }
        cellTravels.selectionStyle = UITableViewCellSelectionStyleNone;
        // 给TravelCell传值
        [cellTravels setModelWithTravelCell:model];
        
        return cellTravels;
    }
    
    else if ([model.type integerValue] == 5 || [model.type integerValue] == 7)
    {
        static NSString *cellW = @"cellWeb";
        WebCell *cellWeb = [tableView dequeueReusableCellWithIdentifier:cellW];
        if (!cellWeb) {
            cellWeb = [[[WebCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellW]autorelease];
        }
        cellWeb.selectionStyle = UITableViewCellSelectionStyleNone;
        [cellWeb setModelWithWebCell:model];
        return cellWeb;
    }
    
    
    
    return nil;
    
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailViewController *detail = [[DetailViewController alloc] init];
    [self.navigationController pushViewController:detail animated:NO];
}


// 网络请求 

- (void)beginConnection
{
    
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:@"5da7664223bc5f2ace51ede9816ff07a" forKey:@"sign"];
    [AsyncGETConnection asyncGETConnectionWithURL:YJ_Bread pamarters:dic block:^(NSData *data) {
       
        NSDictionary *root = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *elementsArr = [root objectForKey:@"elements"];
        
        
        
        
        for (NSDictionary *dtdDic in elementsArr) {
        TravelsModel *model = [[TravelsModel alloc] init];
        HeaderModel *headModel = [[HeaderModel alloc] init];
        [model setValuesForKeysWithDictionary:dtdDic];
        NSArray *dataArr = [dtdDic objectForKey:@"data"];
            
        if ([model.type integerValue] == 1) {
            NSArray *smallArr = [dataArr firstObject];
            for (NSDictionary *advertisementDic in smallArr) {
                [headModel setValuesForKeysWithDictionary:advertisementDic];
                [_headerArray addObject:headModel.image_url];
            }
        }
        
        else if ([model.type integerValue] == 4)
        {
            NSDictionary *moreDic = [dataArr firstObject];
            NSDictionary *userDic = [moreDic objectForKey:@"user"];
            [model setValuesForKeysWithDictionary:moreDic];
            model.personName = [userDic objectForKey:@"name"];
            model.avatar_m = [userDic objectForKey:@"avatar_m"];
            [_tableArray addObject:model];
            
        }
        else if ([model.type integerValue] == 5)
        {
            NSDictionary *typeFiveDic = [dataArr firstObject];
            model.cover_sub_title = [typeFiveDic objectForKey:@"cover_sub_title"];
            model.cover_title = [typeFiveDic objectForKey:@"cover_title"];
            model.url = [typeFiveDic objectForKey:@"url"];
            model.app5_cover_url = [typeFiveDic objectForKey:@"app5_cover_url"];
            [_tableArray addObject:model];
            
        }
        else if ([model.type integerValue] == 7)
        {
            NSDictionary *typeSevenDic = [dataArr firstObject];
            model.cover = [typeSevenDic objectForKey:@"cover"];
            model.sub_title = [typeSevenDic objectForKey:@"sub_title"];
            model.title = [typeSevenDic objectForKey:@"title"];
            [_tableArray addObject:model];
            
        }
           
        
        }
        _header.numArr = _headerArray;
        [_header createScroll];
        [_header setModelWithHeaderView:_headerArray];
            [_tableView reloadData];
    
    }];
    
    
}
    




@end
